package com.iplant.basic.service.po.bfc.auth;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.general.Configuration;
import com.iplant.base.utils.general.DesUtil;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sys.LocalUtils;
import com.iplant.basic.service.basicenum.bfc.BFCAuthCompares;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.bms.BMSRoleItem;
import com.iplant.basic.serviceImpl.BMSServiceImpl;
import com.iplant.basic.utils.Constants;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class BFCAuthRecord
{

    /**
    *  公司名称   申请授权时填写用于记录
    **/

    public String CompanyName ;

    /**
    *  项目名称   申请授权时填写用于记录 系统可自动填写
    **/

    public String ProjectName ;
    /**
    *  软件名称     申请授权时申请系统自动填写
    **/

    public String ProgramName ;
    /**
    *  软件版本    申请授权时申请系统自动填写
    **/

    public String ProgramVersion ;
    /**
    *  服务器IP  申请授权时申请系统自动填写
    **/

    public String IP  = "";

    /**
    *  物理地址 Mac 申请授权时申请系统自动填写
    **/

    public String MacNo  = "";
    /**
    *  服务器名称   申请授权时申请系统自动填写
    **/

    public String ServerName  = "";
    /**
    *  硬盘序列号   申请授权时申请系统自动填写
    **/

    public String DiskNo  = "";
    /**
    *  主板序列号    申请授权时申请系统自动填写
    **/

    public String BoardNo  = "";

    /**
    *  CPU序列号   申请授权时申请系统自动填写
    **/

    public String CPUNo  = "";

    /**
    *  配置文件MD5   无法读取这些值的环境通过配置文件的MD5进行校验  申请授权时申请系统自动填写
    **/

    public String MD5Value  = "";

    /**
    *  授权有效期  申请授权时授权系统填写
    **/ 

    public Calendar ExpireDate  = CalendarTool.getDate();


    /**
    *  模块列表   申请授权时申请系统填写
    **/ 

    public List<BFCModuleAuth> ModuleList  = new ArrayList<>();


    /**
    *  同时在线数    申请授权时授权系统填写
    **/ 

    public int UserNumber  = 0;

    public static BFCAuthRecord GetDefaultValue(String wCompanyName, String wProjectName, String wProgramName, String wProgramVersion){
        BFCAuthRecord wResult = new BFCAuthRecord();

        if (StringUtils.isEmpty(wCompanyName)) {

            wCompanyName = StringUtils.parseString( Configuration.readConfigString("service.company.name",
                    "config/config"));
        }
        if (StringUtils.isEmpty(wProjectName))
        {
            wProjectName = StringUtils.parseString(Configuration.readConfigString("service.project.name",
                    "config/config"));
        }
        if (StringUtils.isEmpty(wProgramName))
        {
            wProgramName = StringUtils.parseString(Configuration.readConfigString("service.program.name",
                    "config/config"));
        }
        if (StringUtils.isEmpty(wProgramVersion))
        {
            wProgramVersion = StringUtils.parseString(Configuration.readConfigString("service.program.version",
                    "config/config"));
        }

        wResult.CompanyName= wCompanyName;

        wResult.ProjectName= wProjectName;
        wResult.ProgramName= wProgramName;
        wResult.ProgramVersion= wProgramVersion;

        List<String> wMacList=new ArrayList<>();
        wResult.IP=StringUtils.Join(",",LocalUtils.GetNetworkInfo(wMacList)) ;
        wResult.MacNo=StringUtils.Join(",",wMacList) ;
        wResult.ServerName= LocalUtils.getShortHostName();
        return wResult;
    }
    public static BFCAuthRecord GetDefaultValue()
    {
        return GetDefaultValue("", "", "", "");
    }


    public  void FullAuthRecord()
    {


        if (StringUtils.isEmpty(this.IP) && StringUtils.isEmpty(this.MacNo) && StringUtils.isEmpty(this.ServerName)
                && StringUtils.isEmpty(this.DiskNo) && StringUtils.isEmpty(this.BoardNo)
                && StringUtils.isEmpty(this.CPUNo))
        {

            BFCAuthRecord wAuthRecord = BFCAuthRecord.GetDefaultValue();

            this.IP = wAuthRecord.IP;
            this.MacNo = wAuthRecord.MacNo;
            this.ServerName = wAuthRecord.ServerName;
            this.DiskNo = wAuthRecord.DiskNo;
            this.BoardNo = wAuthRecord.BoardNo;
            this.CPUNo = wAuthRecord.CPUNo;
            this.MD5Value = wAuthRecord.MD5Value;
        }



        ServiceResult<List<BMSRoleItem>> wServerRst = BMSServiceImpl.getInstance().BMS_GetFunctionNodeTree(BMSEmployee.SysAdmin, 1);

        if (StringUtils.isEmpty(wServerRst.getFaultCode()) && wServerRst.Result != null && wServerRst.Result.size() > 0)
        {
            this.ModuleList = new ArrayList<>();
            BFCModuleAuth wBFCModuleAuth;
            for (BMSRoleItem wBMSRoleItem : wServerRst.Result)
            {
                wBFCModuleAuth = new BFCModuleAuth();

                wBFCModuleAuth.ModuleID = wBMSRoleItem.FunctionID;
                wBFCModuleAuth.ModuleName = wBMSRoleItem.Text;
                wBFCModuleAuth.GroupID = wBMSRoleItem.RoleID;
                wBFCModuleAuth.GroupName = wBMSRoleItem.Remark;
                this.ModuleList.add(wBFCModuleAuth);
            }

        }

        if ((Constants.AuthCompareType &  BFCAuthCompares.MD5.getValue()) > 0)
        {
            //判定MD5文件是否存在，不存在则创建文件，且内容根据时间以及当前硬件信息加密

            String wFilePath =  Constants.getConfigPath() +"/config.properties";

            this.MD5Value = DesUtil.GetFileMD5(wFilePath);

        }

    }




    public  boolean Compare(  BFCAuthRecord wAuthRecord, int wCompareType)
    {
        boolean wResult = true;


        for (BFCAuthCompares wBFCAuthCompares : BFCAuthCompares.values())
        {
            if ((wBFCAuthCompares.getValue() & wCompareType) <= 0)
                continue;


            switch (wBFCAuthCompares)
            {
                case MD5:
                    if (StringUtils.isEmpty(this.MD5Value) || !this.MD5Value.equals(wAuthRecord.MD5Value))
                    {
                        wResult = false;
                        break;
                    }

                    break;
                case CPU:
                    if (StringUtils.isEmpty(this.CPUNo) || !this.CPUNo.equals(wAuthRecord.CPUNo))
                    {
                        wResult = false;
                        break;
                    }
                    break;
                case Board:
                    if (StringUtils.isEmpty(this.BoardNo) || !this.BoardNo.equals(wAuthRecord.BoardNo))
                    {
                        wResult = false;
                        break;
                    }
                    break;
                case Disk:
                    if (StringUtils.isEmpty(this.DiskNo) || !this.DiskNo.equals(wAuthRecord.DiskNo))
                    {
                        wResult = false;
                        break;
                    }
                    break;
                case Mac:
                    if (StringUtils.isEmpty(this.MacNo) || !this.MacNo.equals(wAuthRecord.MacNo))
                    {
                        wResult = false;
                        break;
                    }
                    break;
                case IP:
                    if (StringUtils.isEmpty(this.IP) || !this.IP.equals(wAuthRecord.IP))
                    {
                        wResult = false;
                        break;
                    }
                    break;
                case Server:
                    if (StringUtils.isEmpty(this.ServerName) || !this.ServerName.equals(wAuthRecord.ServerName))
                    {
                        wResult = false;
                        break;
                    }
                    break;
                case Default:
                    break;
                default:
                    break;
            }
            if (!wResult)
                break;
        }
        return wResult;
    }


}
